﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActionPluginAPI;
using System.IO;
using iMapa.Helper;

namespace VlcPlayerRemoteActionPlugin
{
    class VlcPlayerActionPlugin : IActionPlugin
    {
        private static vlcmote.VlcRemote vlc;

        public AbstractActionConfigPanel GetConfigPanel(IActionParameters properties)
        {
            return new VlcPlayerActionConfigPanel(properties);
        }

        public string GetName()
        {
            return "VLC Remote Control";
        }

        public IActionParameters GetNewParametersInstance(string rootPath)
        {
            return new VlcPlayerActionParameters();
        }

        public IActionParameters LoadFromFile(string file)
        {
            VlcPlayerActionParameters parameter = ObjectXMLSerializer<VlcPlayerActionParameters>.Load(file);
            parameter.SetID(new FileInfo(file).Name);
            return parameter;
        }

        public void Execute(IActionParameters parameters)
        {
            VlcPlayerActionParameters p=(VlcPlayerActionParameters) parameters;
            if (p.StartCommand)
            {
                if (vlc == null || vlc.VlcPlaybackProcess == null || vlc.VlcPlaybackProcess.HasExited)
                {
                    if (vlc != null && vlc.client!=null)
                    {
                        vlc.client.Close();
                    }
                    vlc = new vlcmote.VlcRemote(p.Directory);
                }
            }
            else
            {
                vlc.SendCommand(p.Command);
            }
        }
    }
}
